package Demo1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-06-14
 * Time: 15:44
 */
sealed class Animal permits Dog,Cat{
    public String name;
    public int age;

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void eat(){
    }


}

non-sealed class Cat extends Animal{
    public Cat(String name,int age){
        super(name,age);
    }

    public void  eat(){
        System.out.println(this.name + "正在吃猫粮!!!");
    }

    public void mimi(){
        System.out.println(this.name + "正在喵喵叫!!!");
    }
}

non-sealed class Dog extends Animal{
    public Dog(String name,int age){
        super(name,age);
    }
    @Override
    public void eat(){
        System.out.println(this.name + "正在吃狗粮!!!");
    }

    public void wangWang(){
        System.out.println(this.name + "正在汪汪叫!!!");
    }
}
public class Test {
    public static void main2(String[] args) {

    }
    public static void func(Animal animal){
        animal.eat();
//        Cat cat = (Cat)animal;
//        animal.mimi();
//        向下转型：父类类型 -> 子类类型
        if(animal instanceof Animal){
            Cat cat = (Cat)animal;
            cat.mimi();
        }
        if(animal instanceof Cat cat){
            cat.mimi();
        }
        if(animal instanceof Dog dog){
            dog.wangWang();
        }

    }
    public static void main(String[] args) {
//        func(new Dog("大黄",8));
        func(new Cat("大虎",9));
    }
    public static void main1(String[] args) {
        //向上转型的三种形式  直接赋值   参数传递   返回值
        Animal animal1 = new Dog("大黄",8);
        Animal animal2 = new Cat("大虎",7);
        animal1.eat();//发生动态绑定，编译的时候调用父类的方法，但是在运行的时候，父类引用引用了重写的方法名，
        animal2.eat();//发生多态
    }
}
